home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / nevow / context.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-03-23  |  13KB  |  366 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import generators
  5. import warnings
  6. from nevow import stan
  7. from nevow.inevow import IData, IRequest
  8. from nevow.stan import Unset
  9. from nevow.util import qual
  10. from zope.interface import providedBy
  11.  
  12. def megaGetInterfaces(adapter):
  13.     return [ qual(x) for x in providedBy(adapter) ]
  14.  
  15. dataqual = qual(IData)
  16.  
  17. class WebContext(object):
  18.     _remembrances = None
  19.     tag = None
  20.     _slotData = None
  21.     parent = None
  22.     locateHook = None
  23.     isAttrib = property((lambda self: False))
  24.     inURL = property((lambda self: False))
  25.     inJS = property((lambda self: False))
  26.     inJSSingleQuoteString = property((lambda self: False))
  27.     precompile = property((lambda self: False))
  28.     
  29.     def arg(self, get, default = None):
  30.         """Placeholder until I can find Jerub's implementation of this
  31.  
  32.         Return a single named argument from the request arguments
  33.         """
  34.         req = self.locate(IRequest)
  35.         return req.args.get(get, [
  36.             default])[0]
  37.  
  38.     
  39.     def __init__(self, parent = None, tag = None, remembrances = None):
  40.         self.tag = tag
  41.         sd = getattr(tag, 'slotData', None)
  42.         if sd is not None:
  43.             self._slotData = sd
  44.         
  45.         self.parent = parent
  46.         self._remembrances = remembrances
  47.  
  48.     
  49.     def remember(self, adapter, interface = None):
  50.         """Remember an object that implements some interfaces.
  51.         Later, calls to .locate which are passed an interface implemented
  52.         by this object will return this object.
  53.  
  54.         If the 'interface' argument is supplied, this object will only
  55.         be remembered for this interface, and not any of
  56.         the other interfaces it implements.
  57.         """
  58.         if interface is None:
  59.             interfaceList = megaGetInterfaces(adapter)
  60.             if not interfaceList:
  61.                 interfaceList = [
  62.                     dataqual]
  63.             
  64.         else:
  65.             interfaceList = [
  66.                 qual(interface)]
  67.         if self._remembrances is None:
  68.             self._remembrances = { }
  69.         
  70.         for interface in interfaceList:
  71.             self._remembrances[interface] = adapter
  72.         
  73.         return self
  74.  
  75.     
  76.     def locate(self, interface, depth = 1, _default = object()):
  77.         '''Locate an object which implements a given interface.
  78.         Objects will be searched through the context stack top
  79.         down.
  80.         '''
  81.         key = qual(interface)
  82.         currentContext = self
  83.         while True:
  84.             if depth < 0:
  85.                 full = []
  86.                 while True:
  87.                     
  88.                     try:
  89.                         full.append(self.locate(interface, len(full) + 1))
  90.                     continue
  91.                     except KeyError:
  92.                         break
  93.                         continue
  94.                     
  95.  
  96.                     None<EXCEPTION MATCH>KeyError
  97.                 if full:
  98.                     return full[depth]
  99.                 return None
  100.             _remembrances = currentContext._remembrances
  101.             if _remembrances is not None:
  102.                 rememberedValue = _remembrances.get(key, _default)
  103.                 if rememberedValue is not _default:
  104.                     depth -= 1
  105.                     if not depth:
  106.                         return rememberedValue
  107.                 
  108.             
  109.             locateHook = currentContext.locateHook
  110.             if locateHook is not None:
  111.                 result = locateHook(interface)
  112.                 if result is not None:
  113.                     currentContext.remember(result, interface)
  114.                     return result
  115.             
  116.             contextParent = currentContext.parent
  117.             if contextParent is None:
  118.                 raise KeyError, 'Interface %s was not remembered.' % key
  119.             contextParent is None
  120.             currentContext = contextParent
  121.  
  122.     
  123.     def chain(self, context):
  124.         """For nevow machinery use only.
  125.  
  126.         Go to the top of this context's context chain, and make
  127.         the given context the parent, thus continuing the chain
  128.         into the given context's chain.
  129.         """
  130.         top = self
  131.         while top.parent is not None:
  132.             if top.parent.tag is None:
  133.                 break
  134.             
  135.             top = top.parent
  136.             if top is context:
  137.                 return None
  138.             continue
  139.             top is context
  140.         top.parent = context
  141.  
  142.     
  143.     def fillSlots(self, name, stan):
  144.         """Set 'stan' as the stan tree to replace all slots with name 'name'.
  145.         """
  146.         if self._slotData is None:
  147.             self._slotData = { }
  148.         
  149.         self._slotData[name] = stan
  150.  
  151.     
  152.     def locateSlotData(self, name):
  153.         '''For use by nevow machinery only, or for some fancy cases.
  154.  
  155.         Find previously remembered slot filler data.
  156.         For use by flatstan.SlotRenderer'''
  157.         currentContext = self
  158.         while True:
  159.             if currentContext._slotData:
  160.                 data = currentContext._slotData.get(name, Unset)
  161.                 if data is not Unset:
  162.                     return data
  163.             
  164.             if currentContext.parent is None:
  165.                 raise KeyError, "Slot named '%s' was not filled." % name
  166.             currentContext.parent is None
  167.             currentContext = currentContext.parent
  168.  
  169.     
  170.     def clone(self, deep = True, cloneTags = True):
  171.         if self.parent is not None:
  172.             if deep:
  173.                 parent = self.parent.clone(cloneTags = False)
  174.             else:
  175.                 parent = self.parent
  176.         else:
  177.             parent = None
  178.         if cloneTags:
  179.             tag = self.tag.clone(deep = deep)
  180.         else:
  181.             tag = self.tag
  182.         if self._remembrances is not None:
  183.             remembrances = self._remembrances.copy()
  184.         else:
  185.             remembrances = None
  186.         return type(self)(parent = parent, tag = tag, remembrances = remembrances)
  187.  
  188.     
  189.     def __conform__(self, interface):
  190.         '''Support IFoo(ctx) syntax.
  191.         '''
  192.         
  193.         try:
  194.             return self.locate(interface)
  195.         except KeyError:
  196.             return None
  197.  
  198.  
  199.     
  200.     def __repr__(self):
  201.         rstr = ''
  202.         if self._remembrances:
  203.             rstr = ', remembrances=%r' % self._remembrances
  204.         
  205.         return '%s(tag=%r%s)' % (self.__class__.__name__, self.tag, rstr)
  206.  
  207.  
  208.  
  209. class FactoryContext(WebContext):
  210.     '''A context which allows adapters to be registered against it so that an object
  211.     can be lazily created and returned at render time. When ctx.locate is called
  212.     with an interface for which an adapter is registered, that adapter will be used
  213.     and the result returned.
  214.     '''
  215.     inLocate = 0
  216.     
  217.     def locateHook(self, interface):
  218.         self.inLocate += 1
  219.         adapter = interface(self, None)
  220.         self.inLocate -= 1
  221.         return adapter
  222.  
  223.     
  224.     def __conform__(self, interface):
  225.         if self.inLocate:
  226.             return None
  227.         return WebContext.__conform__(self, interface)
  228.  
  229.  
  230.  
  231. class SiteContext(FactoryContext):
  232.     '''A SiteContext is created and installed on a NevowSite upon initialization.
  233.     It will always be used as the root context, and can be used as a place to remember
  234.     things sitewide.
  235.     '''
  236.     pass
  237.  
  238.  
  239. class RequestContext(FactoryContext):
  240.     '''A RequestContext has adapters for the following interfaces:
  241.  
  242.     ISession
  243.     IFormDefaults
  244.     IFormErrors
  245.     IHand
  246.     IStatusMessage
  247.     '''
  248.     pass
  249.  
  250.  
  251. def getRequestContext(self):
  252.     top = self.parent
  253.     while not isinstance(top, RequestContext):
  254.         top = top.parent
  255.     return top
  256.  
  257.  
  258. class PageContext(FactoryContext):
  259.     '''A PageContext has adapters for the following interfaces:
  260.  
  261.     IRenderer
  262.     IRendererFactory
  263.     IData
  264.     '''
  265.     
  266.     def __init__(self, *args, **kw):
  267.         FactoryContext.__init__(self, *args, **kw)
  268.         if self.tag is not None and hasattr(self.tag, 'toremember'):
  269.             for i in self.tag.toremember:
  270.                 self.remember(*i)
  271.             
  272.         
  273.  
  274.  
  275. NodeNotFound = stan.NodeNotFound
  276. TooManyNodes = stan.TooManyNodes
  277.  
  278. class WovenContext(WebContext):
  279.     key = None
  280.     isAttrib = False
  281.     inURL = False
  282.     precompile = False
  283.     inJS = False
  284.     inJSSingleQuoteString = False
  285.     
  286.     def __init__(self, parent = None, tag = None, precompile = None, remembrances = None, key = None, isAttrib = None, inURL = None, inJS = None, inJSSingleQuoteString = None):
  287.         WebContext.__init__(self, parent, tag, remembrances)
  288.         if self.parent:
  289.             self.precompile = parent.precompile
  290.             self.isAttrib = parent.isAttrib
  291.             self.inURL = parent.inURL
  292.             self.inJS = parent.inJS
  293.             self.inJSSingleQuoteString = parent.inJSSingleQuoteString
  294.         
  295.         if self.tag is not None:
  296.             if self.tag.remember is not Unset:
  297.                 self.remember(tag.remember)
  298.             
  299.             if key is None:
  300.                 key = self.tag.key
  301.             
  302.         
  303.         if key is not None and key is not Unset:
  304.             if self.parent is not None and getattr(self.parent, 'key', None):
  305.                 self.key = '.'.join((self.parent.key, key))
  306.             else:
  307.                 self.key = key
  308.         elif self.parent is not None:
  309.             self.key = getattr(self.parent, 'key', '')
  310.         
  311.         if precompile is not None:
  312.             self.precompile = precompile
  313.         
  314.         if isAttrib is not None:
  315.             self.isAttrib = isAttrib
  316.         
  317.         if inURL is not None:
  318.             self.inURL = inURL
  319.         
  320.         if inJS is not None:
  321.             self.inJS = inJS
  322.         
  323.         if inJSSingleQuoteString is not None:
  324.             self.inJSSingleQuoteString = inJSSingleQuoteString
  325.         
  326.  
  327.     
  328.     def __repr__(self):
  329.         rstr = ''
  330.         if self._remembrances:
  331.             rstr = ', remembrances=%r' % self._remembrances
  332.         
  333.         attribstr = ''
  334.         if self.isAttrib:
  335.             attribstr = ', isAttrib=True'
  336.         
  337.         urlStr = ''
  338.         if self.inURL:
  339.             urlStr = ', inURL=True'
  340.         
  341.         return '%s(tag=%r%s%s%s)' % (self.__class__.__name__, self.tag, rstr, attribstr, urlStr)
  342.  
  343.     
  344.     def patternGenerator(self, pattern, default = None):
  345.         warnings.warn('use Tag.patternGenerator instead', DeprecationWarning, stacklevel = 2)
  346.         return self.tag.patternGenerator(pattern, default)
  347.  
  348.     
  349.     def allPatterns(self, pattern):
  350.         warnings.warn('use Tag.allPatterns instead', DeprecationWarning, stacklevel = 2)
  351.         return self.tag.allPatterns(pattern)
  352.  
  353.     
  354.     def onePattern(self, pattern):
  355.         warnings.warn('use Tag.onePattern instead', DeprecationWarning, stacklevel = 2)
  356.         return self.tag.onePattern(pattern)
  357.  
  358.     
  359.     def clone(self, deep = True, cloneTags = True):
  360.         cloned = WebContext.clone(self, deep, cloneTags)
  361.         cloned.isAttrib = self.isAttrib
  362.         cloned.inURL = self.inURL
  363.         return cloned
  364.  
  365.  
  366.